package com.ruoyi.web.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.BCase;
import com.ruoyi.system.domain.BTask;
import com.ruoyi.system.domain.BTaskUser;
import com.ruoyi.system.domain.BUserScore;
import com.ruoyi.system.domain.request.CaseRequest;
import com.ruoyi.system.domain.request.SignRequest;
import com.ruoyi.system.domain.vo.BCaseVo;
import com.ruoyi.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/case")
public class CaseController extends BaseController {

    @Autowired
    BCaseService caseService;

    @Autowired
    BCaseConferenceService caseConferenceService;

    @Autowired
    BTaskService taskService;

    @Autowired
    BTaskUserService taskUserService;

    @Autowired
    BUserScoreService userScoreService;

    /**
     * 分页获取案例列表
     */
    @GetMapping("/list")
    public TableDataInfo queryList(CaseRequest request){
        request.setStatus("1");
        startPage();
        List<BCaseVo> list = caseService.queryList(request);
        return getDataTable(list);
    }

    /**
     * 根据案例Id获取详细信息
     */
    @GetMapping(value = "/{caseId}")
    public AjaxResult getInfo(@PathVariable Long caseId) {
        BCaseVo caseVo = caseService.selectCaseById(caseId);
        return success(caseVo);
    }

    /**
     * 创建案例
     */
    @PostMapping
    public AjaxResult create(@Validated @RequestBody BCaseVo bCaseVo){
        BCase bCase = new BCase();
        BeanUtils.copyProperties(bCaseVo, bCase);
        bCase.setTransfer(bCaseVo.getChange());
        bCase.setUserId(SecurityUtils.getUserId());
        bCase.setCreateBy(SecurityUtils.getUsername());
        bCase.setCreateTime(new Date());
        caseService.save(bCase);
        if(ObjectUtils.isNotEmpty(bCase.getTaskId())){
            //更新关联关系及积分
            BTask bTask = taskService.getById(bCase.getTaskId());
            BTaskUser taskUser = taskUserService.getOne(new QueryWrapper<BTaskUser>().lambda()
                    .eq(BTaskUser::getTaskId,bCase.getTaskId())
                    .eq(BTaskUser::getUserId, SecurityUtils.getUserId()));
            if(ObjectUtils.isEmpty(taskUser)){
                taskUser = new BTaskUser();
                taskUser.setUserId(SecurityUtils.getUserId());
                taskUser.setTaskId(bCase.getTaskId());
                taskUser.setTotalNum(1);
                taskUser.setCreateTime(new Date());
                taskUser.setCreateBy(SecurityUtils.getUsername());
                taskUserService.save(taskUser);
            }else{
                taskUser.setUpdateTime(new Date());
                taskUser.setUpdateBy(SecurityUtils.getUsername());
                taskUser.setTotalNum(taskUser.getTotalNum()+1);
                taskUserService.updateById(taskUser);
            }
            BUserScore userScore = new BUserScore();
            userScore.setUserId(SecurityUtils.getUserId());
            userScore.setTaskId(bCase.getTaskId());
            userScore.setScore(bTask.getScore());
            userScore.setType(1);
            userScore.setCreateTime(new Date());
            userScore.setCreateBy(SecurityUtils.getUsername());
            userScoreService.save(userScore);
        }
        return AjaxResult.success(MapUtil.builder().put("id",bCase.getId()).build());
    }

    /**
     * 创建案例
     */
    @PostMapping("/patient/create")
    public AjaxResult patientCreate(@Validated @RequestBody BCase bCase){
        bCase.setCreateTime(new Date());
        caseService.save(bCase);
        return AjaxResult.success(MapUtil.builder().put("id",bCase.getId()).build());
    }
    /**
     * 报名研讨会
     * @return
     */
    @PostMapping(value = "/sign")
    public AjaxResult sign(SignRequest request){
        Boolean result = caseConferenceService.sign(request);
        if(result){
            return AjaxResult.success("报名成功");
        }else{
            return AjaxResult.error("报名失败");
        }
    }
}
